import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/common/http.dart';
import 'package:get/get.dart';

import 'state.dart';

class SupportLogic extends GetxController
    with GetSingleTickerProviderStateMixin {
  final state = SupportState();
  fetchCate() async {
    var res = await Http.instance.post('/plugin/help_center/api/cate');
    if (res.code == 1) {
      state.category = res.data;
      if (!state.isInit) {
        state.tabController =
            TabController(length: state.category.length, vsync: this);
        state.isInit = true;
      }
    }
    fetchList();
    update(['category']);
  }

  fetchList() async {
    state.loading = true;
    update(['questions']);
    var res = await Http.instance.post('/plugin/help_center/api/index', data: {
      "cate_id":
          state.category.isEmpty ? 0 : state.category[state.currentIndex]['id'],
      "keyword": state.keyword,
    });
    state.uiStateController.setStatus(res);
    if (res.code == 1) {
      state.questions.clear();
      res.data['data'].forEach((item) => state.questions.add(item));
    }
    state.loading = false;
    update(['questions']);
  }

  onTabsClick(index) {
    state.searchBarController.unfocus();
    state.currentIndex = index;
    fetchList();
  }

  search(keyword) {
    state.keyword = keyword;
    fetchList();
  }
}
